// index.js
// 获取应用实例
const app = getApp()
const { APPID, SECRET, MCHID } = require('../../utils/util')

Page({
  data: {
    list: [],
    cates: [],
    toView: 'view1',
    currentCategory: 0,
    isFixed: false,
    foods: [],
    isShowJiesuan: false
  },
  onLoad: async function (options) {
    await this.getFoods()
    await this.test()
  },
  onShow: async function () {
    console.log('onShow')
    if (this.data.foods.length <= 0 || app.order.list.length == 0) {
      await this.getFoods()
    }
    console.log(app.order.list, 'app.order.list')
    let list = app.order.list
    let foods = this.data.foods
    console.log(foods, 'foods')

    foods.forEach(item => {
      if (item.list) {
        item.list.forEach(async item3 => {
          list.forEach(item2 => {
            if (item3.id == item2.id) {
              item3.count = item2.count
            }
          })
        })
      } else {
        item.list = []
      }
    })
    this.setData({
      foods
    })
  },
  async getFoods() {
    console.log('onload')
    const res = await app._http({
      url: 'food/getlistwithcate',
      // data: { store: '1' }
    })
    if (res.data.code === 1) {
      let totalFoods = res.data.foods
      this.setData({
        hots: (totalFoods.find(e => e.name === '热门')).list,
        foods: totalFoods,
        cates: res.data.cates,
      })
    }
  },
  gotoView(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.setData({
      toView: `view${index}`,
      currentCategory: index
    })
  },
  ordergoods(e) {
    const {
      item
    } = e.currentTarget.dataset
    app.order.add(item)
    console.log(app.order.list)

    let list = this.data.foods
    list.forEach(li => {
      if (li.catid == item.catid) {

        li.list.forEach(ilitem => {
          if (ilitem.id == item.id) {
            console.log('找到了');
            if (ilitem.count) {
              ilitem.count++
            } else {
              ilitem.count = 1
            }
          }
        })
      }
    });
    this.setData({
      foods: list,
    })

  },
  decrementgoods(e) {
    const {
      item
    } = e.currentTarget.dataset
    app.order.delete(item)

    let list = this.data.foods
    list.forEach(li => {
      if (li.catid == item.catid) {
        li.list.forEach(ilitem => {
          if (ilitem.id == item.id) {
            if (ilitem.count >= 1) {
              ilitem.count--
            }
          }
        })
      }
    });
    console.log(list.length, 'length');
    
    this.setData({
      foods: list,
    })
  },

  async gobuy() {
    console.log(app.globalData.userinfo);
    let userinfo = wx.getStorageSync('logininfo')
    let isLogin = wx.getStorageSync('isLogin')
    
    if (isLogin && userinfo.openid) {
      wx.navigateTo({
        url: '/pages/shop/shop',
      })
    }else{
      wx.showModal({
        content: '请登录',
        confirmColor: app.PCOLOR,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/mine/mine',
            })
          }
        }
      })
    }





    // wx.switchTab({
    //   url: '/pages/shop/shop',
    // })
  },

  async test(){
    console.log('res');
    let data = {
      appid: APPID,
      mchid: MCHID,
      description: '为美食买单~'
    }
    const res = await app._http({
      url: 'wxpay/createorder',
      data
    })
    console.log(res, 'res');
    
    
  },

  zhizuo(){
    // this.isShow({

    // })
  }
})

